﻿Imports Microsoft.Msdn.Samples.BITS

Public Class FrmFile

    Private Job As Job 'The job which contains the current file
    Private File As BITSFile 'The displayed file

    Public Const MODE_DISPLAY As Byte = 1 'Open file in display mode
    Public Const MODE_CHANGE As Byte = 2 'Open file in change mode (For further development)

    ''' <summary>
    ''' Uncheckes all View->Refresh speed menu entries
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub _UncheckMenuEntries()
        MnuViewUpdatespeedHigh.Checked = False
        MnuViewUpdatespeedNever.Checked = False
        MnuViewUpdatespeedNormal.Checked = False
        MnuViewUpdatespeedSlow.Checked = False
        MnuViewUpdatespeedVeryHigh.Checked = False
    End Sub

    ''' <summary>
    ''' Loads the provided file from the job and displays the information about it
    ''' </summary>
    ''' <param name="mode">Provides the mode (display/change) of this form</param>
    ''' <param name="JobNumber">The job where the file is contained in</param>
    ''' <param name="FileNumber">The file number within the job</param>
    ''' <remarks></remarks>
    Public Sub _Show(ByVal mode As Byte, ByVal JobNumber As Integer, ByVal FileNumber As Integer)
        Dim Jobs As JobCollection 'List of jobs
        Jobs = ModBITS.BITS.GetListofJobs 'Get the list of jobs from BITS
        Job = Jobs(JobNumber) 'Extracts the job from the list
        File = Job.Files(FileNumber) 'Extracts the file from the job
        _ReadFile() 'Call the sub for displaying the file information
        Me.Show() 'Displays this form
    End Sub

    ''' <summary>
    ''' Enters/Updates all information for this form
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub _ReadFile()
        LblLocalValue.Text = File.LocalName
        LblRemoteValue.Text = File.RemoteName
        LblBytesTotal.Text = Strings.Format(File.Progress.BytesTotal, "##,##0")
        LblBytesTransferred.Text = Strings.Format(File.Progress.BytesTransferred, "##,##0")
        LblTransferredValue.Text = File.Progress.Completed.ToString
        If File.Progress.BytesTotal > 2147483647 Then
            Dim IntFactor As New UInt32
            IntFactor = Math.Ceiling(Job.Progress.BytesTotal / 2147483647)
            PrgBytes.Maximum = Math.Ceiling(Job.Progress.BytesTotal / IntFactor)
            PrgBytes.Value = Math.Ceiling(Job.Progress.BytesTransferred / IntFactor)
        Else
            PrgBytes.Maximum = File.Progress.BytesTotal
            PrgBytes.Value = File.Progress.BytesTransferred
        End If
        
        LblParentJobDesc.Text = Job.Description
        LblParentJobName.Text = Job.DisplayName
    End Sub

    Private Sub CmdOKCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub FrmFile_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _UncheckMenuEntries()
        Select Case My.Settings.Refresh
            Case Is = 0
                TimRefresh.Enabled = False
                MnuViewUpdatespeedNever.Checked = True
            Case Is = 300
                TimRefresh.Interval = 300
                TimRefresh.Enabled = True
                MnuViewUpdatespeedVeryHigh.Checked = True
            Case Is = 800
                TimRefresh.Interval = 800
                TimRefresh.Enabled = True
                MnuViewUpdatespeedHigh.Checked = True
            Case Is = 1500
                TimRefresh.Interval = 1500
                TimRefresh.Enabled = True
                MnuViewUpdatespeedNormal.Checked = True
            Case Is = 3000
                TimRefresh.Interval = 3000
                TimRefresh.Enabled = True
                MnuViewUpdatespeedSlow.Checked = True
            Case Else
                TimRefresh.Interval = My.Settings.Refresh
                TimRefresh.Enabled = True
        End Select
    End Sub

    Private Sub TimRefresh_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimRefresh.Tick
        _ReadFile()
    End Sub

    Private Sub MnuViewUpdateNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuViewUpdateNow.Click
        _ReadFile()
    End Sub

    Private Sub MnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuFileExit.Click
        Me.Close()
    End Sub

    Private Sub MnuViewUpdatespeedVeryHigh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuViewUpdatespeedVeryHigh.Click
        TimRefresh.Interval = 300
        TimRefresh.Start()
        _UncheckMenuEntries()
        MnuViewUpdatespeedVeryHigh.Checked = True
    End Sub

    Private Sub MnuViewUpdatespeedHigh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuViewUpdatespeedHigh.Click
        TimRefresh.Interval = 800
        TimRefresh.Start()
        _UncheckMenuEntries()
        MnuViewUpdatespeedHigh.Checked = True
    End Sub

    Private Sub MnuViewUpdatespeedNormal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuViewUpdatespeedNormal.Click
        TimRefresh.Interval = 1500
        TimRefresh.Start()
        _UncheckMenuEntries()
        MnuViewUpdatespeedNormal.Checked = True
    End Sub

    Private Sub MnuViewUpdatespeedSlow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuViewUpdatespeedSlow.Click
        TimRefresh.Interval = 3000
        TimRefresh.Start()
        _UncheckMenuEntries()
        MnuViewUpdatespeedSlow.Checked = True
    End Sub

    Private Sub MnuViewUpdatespeedNever_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuViewUpdatespeedNever.Click
        TimRefresh.Stop()
        _UncheckMenuEntries()
        MnuViewUpdatespeedSlow.Checked = True
    End Sub
End Class